#Include <ScreenCapture.au3>
#Include <File.au3>
#Include <Function For Performance.au3>
#Include <GuiListView.au3>
#include <Date.au3>
#Include <..\Scan\Function For Scan.au3>

Common_WriteCurrentFunctionNameToTempTXT('Performance_Scan Button')
CloseOpenedExcel($oExcel)
Performance8_2_1()

Func Performance8_2_1()
	Local $TestCaseID = "8.2.1 - (Scan Button Performance)", $hScanSettingDialog = '', $hThumbnailView = '', $sCaptureImage = $TestCaseID & '.jpg', $hPMHandle = '', $bReproduceTestCase = False 
	If Assign('bReproduceTestCase', Common_ReproduceRunWithIni($sFunctionNamePerformance,$TestCaseID)) And $bReproduceTestCase == False Then Return 3
	Common_Del_DB()
	Common_InitializeStatus()
	InitializeStatusForPerformance_ScanButton()
	If Assign('hPMHandle', Common_Run_PageManager_Ex($TestCaseID, $sFunctionNamePerformance)) And $hPMHandle == '' Then Return False
	If Not ClickPMSBButtonInToolBar($hPMHandle,$TestCaseID, $sFunctionNamePerformance) Then Return False 

	Local $iSysTimeBegin = _NowCalcDate() & ' ' & _NowTime(5)
	Local $iMemoryBegin = GetProcessPrivateMemory($sPMProcessName)
	Local $iHanldesBegin = GetProcessHandles($sPMProcessName)
	Local $iGDIObjectsBegin = GetProcessGDIObjects ($sPMProcessName)
	Local $iCPUUsageBegin = GetProcessCPUUseage($sPMProcessName)

	For $i = 1 to Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameScanButton, 'ScanTimes'))
		If Not ClickButtonInPMSB('Scan to PM', $TestCaseID, $sFunctionNamePerformance) Then Return False 
		If Not WaitScanFinished($sScanningProgressBarDlgTitle, 120, $TestCaseID, $sFunctionNamePerformance) Then Return False	
		Common_SleepTime(10)
	Next	
	
	Local $iTotalTime = _DateDiff( 's', $iSysTimeBegin, _NowCalc())
	Local $iSysTimeAfter = _NowCalcDate() & ' ' & _NowTime(5)
	Local $iMemoryAfter = GetProcessPrivateMemory($sPMProcessName)
	Local $iHanldesAfter = GetProcessHandles($sPMProcessName)
	Local $iGDIObjectsAfter = GetProcessGDIObjects ($sPMProcessName)
	Local $iCPUUsageAfter = GetProcessCPUUseage($sPMProcessName)
	
	Common_Close_PM($hPMHandle)

;--------------------------------Second Time--------------------------------------------	
	Common_Del_DB()
	Common_InitializeStatus()
	InitializeStatusForPerformance_ScanButton()
	If Assign('hPMHandle', Common_Run_PageManager_Ex($TestCaseID, $sFunctionNamePerformance)) And $hPMHandle == '' Then Return False
	If Not ClickPMSBButtonInToolBar($hPMHandle,$TestCaseID, $sFunctionNamePerformance) Then Return False
		
	Local $iSysTimeBegin_ = _NowCalcDate() & ' ' & _NowTime(5)
	Local $iMemoryBegin_ = GetProcessPrivateMemory($sPMProcessName)
	Local $iHanldesBegin_ = GetProcessHandles($sPMProcessName)
	Local $iGDIObjectsBegin_ = GetProcessGDIObjects ($sPMProcessName)
	Local $iCPUUsageBegin_ = GetProcessCPUUseage($sPMProcessName)		
		
	For $i = 1 to Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameScanButton, 'ScanTimes'))
		If Not ClickButtonInPMSB('Scan to PM', $TestCaseID, $sFunctionNamePerformance) Then Return False 
		If Not WaitScanFinished($sScanningProgressBarDlgTitle, 120, $TestCaseID, $sFunctionNamePerformance) Then Return False	
		Common_SleepTime(10)
	Next		
	
	Local $iTotalTime_ = _DateDiff( 's', $iSysTimeBegin_, _NowCalc())
	Local $iSysTimeAfter_ = _NowCalcDate() & ' ' & _NowTime(5)
	Local $iMemoryAfter_ = GetProcessPrivateMemory($sPMProcessName)
	Local $iHanldesAfter_ = GetProcessHandles($sPMProcessName)
	Local $iGDIObjectsAfter_ = GetProcessGDIObjects ($sPMProcessName)
	Local $iCPUUsageAfter_ = GetProcessCPUUseage($sPMProcessName)	
	

	Local $bResult = True, $bTime = False, $bMeomory = False, $bHandles= False, $bGDI = False, $bCPU = False

	If $iTotalTime_ < ($iTotalTime - $iScanButtonTime_MarginOfError) Or $iTotalTime_ > ($iTotalTime + $iScanButtonTime_MarginOfError) Then
		$bResult = False
		$bTime = True
	EndIf
	
	If $iMemoryAfter_ < ($iMemoryAfter - $iScanButtonMemory_MarginOfError) Or $iMemoryAfter_ > ($iMemoryAfter + $iScanButtonMemory_MarginOfError) Then
		$bResult = False
		$bMeomory = True
	EndIf

	If $iHanldesAfter_ < ($iHanldesAfter - $iScanButtonHandles_MarginOfError) Or $iHanldesAfter_ > ($iHanldesAfter + $iScanButtonHandles_MarginOfError) Then
		$bResult = False
		$bHandles = True
	EndIf

	If $iGDIObjectsAfter_ < ($iGDIObjectsAfter - $iScanButtonGDIObjects_MarginOfError) Or $iGDIObjectsAfter_ > ($iGDIObjectsAfter + $iScanButtonGDIObjects_MarginOfError) Then
		$bResult = False
		$bGDI = True
	EndIf

	If $iCPUUsageAfter_ < ($iCPUUsageAfter - $iScanButtonCPUUsage_MarginOfError) Or $iCPUUsageAfter_ > ($iCPUUsageAfter + $iScanButtonCPUUsage_MarginOfError) Then
		$bResult = False
		$bCPU = True
	EndIf
	
	If $bResult Then 
		Common_WriteLog($sPass, $TestCaseID, $sFunctionNamePerformance, $bReproduceTestCase)
	Else
		Common_WriteLog($sFaile, $TestCaseID, $sFunctionNamePerformance, $bReproduceTestCase)
		If $bTime Then 
			WriteTextCaseInfo ('First Time Scan Button  - Total time: ' & $iTotalTime, $sFunctionNamePerformance)
			WriteTextCaseInfo ('Second Time Scan Button - Total time: ' & $iTotalTime_, $sFunctionNamePerformance)
		EndIf	
			
		If $bMeomory Then 
			WriteTextCaseInfo ('First Time Scan Button  - Memory: ' & $iMemoryAfter, $sFunctionNamePerformance)
			WriteTextCaseInfo ('Second Time Scan Button - Memory: ' & $iMemoryAfter_, $sFunctionNamePerformance)
		EndIf
		
		If $bHandles Then 
			WriteTextCaseInfo ('First Time Scan Button  - Hanldes: ' & $iHanldesAfter, $sFunctionNamePerformance)
			WriteTextCaseInfo ('Second Time Scan Button - Hanldes: ' & $iHanldesAfter_, $sFunctionNamePerformance)
		EndIf	
		
		If $bGDI Then 
			WriteTextCaseInfo ('First Time Scan Button  - GDIObjects: ' & $iGDIObjectsAfter, $sFunctionNamePerformance)
			WriteTextCaseInfo ('Second Time Scan Button - GDIObjects: ' & $iGDIObjectsAfter_, $sFunctionNamePerformance)
		EndIf
		
		If $bCPU Then 
			WriteTextCaseInfo ('First Time Scan Button  - CPUUsage: ' & $iCPUUsageAfter, $sFunctionNamePerformance)
			WriteTextCaseInfo ('Second Time Scan Button - CPUUsage: ' & $iCPUUsageAfter_, $sFunctionNamePerformance)
		EndIf
	EndIf

	Common_Close_PM($hPMHandle)	

EndFunc

